package cn.iocoder.yudao.module.bpm.framework.flowable.core.listener.demo.exection;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.magic.api.MagicApiExcuse;
import cn.iocoder.yudao.framework.magic.api.dto.ExcuseMagicApiDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.flowable.bpmn.model.FieldExtension;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.JavaDelegate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;

/**
 * 类型为 magicApiDelegateExpressionExecutionListener 的 ExecutionListener 监听器示例
 *
 * 和 {@link DemoDelegateClassExecutionListener} 的差异是，需要注册到 Spring 中
 */
@Component
@Slf4j
public class MagicApiDelegateExpressionExecutionListener implements JavaDelegate {

    @Autowired
    MagicApiExcuse magicApiExcuse ;


    @Override
    public void execute(DelegateExecution execution) {

        AtomicReference<String> method = new AtomicReference<>();
        AtomicReference<String> path = new AtomicReference<>();
        Map<String, Object> context = new HashMap<>();

        log.info("[execute][execution({}) 被调用]", execution.getId());
        List<FieldExtension> fieldExtensions = execution.getCurrentFlowableListener().getFieldExtensions();
        fieldExtensions.stream().forEach(fieldExtension ->{
                log.info("[execute]变量有" +
                                "id：{}，" +
                                "fieldName：{}，" +
                                "stringValue：{}，" +
                                "expression：{}，",
                        fieldExtension.getId(),
                        fieldExtension.getFieldName(),
                        fieldExtension.getStringValue(),
                        fieldExtension.getExpression()
                );
                String value = StringUtils.isNoneBlank(fieldExtension.getStringValue()) ? fieldExtension.getStringValue() : fieldExtension.getExpression();
                if("method".equals(fieldExtension.getFieldName())){
                        method.set(value);
                }else if("path".equals(fieldExtension.getFieldName())){
                    path.set(value);
                }else {
                    context.put(fieldExtension.getFieldName(), value);
                }
            }
        );
        log.info("[execute]" +
                        "流程实例ID：{}，业务键：{}，活动（节点）ID：{}，事件类型：{}，流程变量：{}。",
                execution.getProcessInstanceId(),
                execution.getProcessInstanceBusinessKey(),
                execution.getCurrentActivityId(),
                execution.getEventName(),
                execution.getVariables()
        );

        context.put("processInstanceId", execution.getProcessInstanceId());
        context.put("businessKey", execution.getProcessInstanceBusinessKey());
        context.put("taskDefinitionKey", execution.getCurrentActivityId());
        context.put("eventName", execution.getEventName());

        context.putAll(execution.getVariables());

        ExcuseMagicApiDTO excuseMagicApiDTO = new ExcuseMagicApiDTO();
        excuseMagicApiDTO.setMethod(method.get());
        excuseMagicApiDTO.setPath(path.get());
        excuseMagicApiDTO.setContext(context);

        magicApiExcuse.excuseMagicApi(excuseMagicApiDTO);
    }

}
